package com.capgemini.scaffold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.mapper.UserMapper;
import com.capgemini.scaffold.model.Dict;

import com.capgemini.scaffold.model.request.DictAddReq;
import com.capgemini.scaffold.model.request.DictQueryReq;
import com.capgemini.scaffold.service.DictService;
import com.capgemini.scaffold.mapper.DictMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* @author 11470
* @description 针对表【dict(字典表)】的数据库操作Service实现
* @createDate 2022-10-18 16:20:04
*/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
    implements DictService{

    @Resource
    DictMapper dictMapper;

    /**
     * 获取position列表
     *
     * @return
     */
    @Override
    public List<Dict> getPosition() {
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dict_group","职位");
        return dictMapper.selectList(queryWrapper);
    }

    /**
     * 获取department列表
     *
     * @return
     */
    @Override
    public List<Dict> getDepartment() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_group","部门");
        return dictMapper.selectList(queryWrapper);
    }

    /**
     * 获取level列表
     *
     * @return
     */
    @Override
    public List<Dict> getLevel() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_group","职级");
        return dictMapper.selectList(queryWrapper);
    }

    /**
     * 根据字典名，字典类型和字典创建时间查询
     *
     * @param dictReq
     * @return
     */
    @Override
    public List<Dict> queryDict(DictQueryReq dictReq) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("dict_name",dictReq.getDictName())
                .like("dict_group",dictReq.getDictGroup())
                .like("dict_create_time",dictReq.getDictCreateTime())
                .eq("dict_is_delete",0);
        int count = dictMapper.selectCount(queryWrapper);
        if(count == 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"找不到该字典");
        }
        return dictMapper.selectList(queryWrapper);
    }

//    /**
//     * 空参查询
//     *
//     * @return
//     */
//    @Override
//    public List<Dict> queryAll() {
//        return dictMapper.selectList(null);
//    }

    /**
     * 获取字典类型列表
     *
     * @return
     */
    public List<String> getDictGroup(){
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        List<Dict> dictList = dictMapper.selectList(queryWrapper.select("dict_group"));
        List<String> list=new ArrayList<>();
        for (Dict dict : dictList) {
            list.add(dict.getDictGroup());
        }
        list = list.stream()
                   .distinct()
                   .collect(Collectors.toList());
        return list;
    }

    /**
     * 添加字典
     *
     * @param dictAddReq
     * @return
     */
    @Override
    public Dict addDict(DictAddReq dictAddReq) {
        // 获取系统当前时间，即为默认创建时间和更新时间
        Date date = new Date();
        //查重
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_id",dictAddReq.getDictId());
        long count = dictMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"该字典已经存在");
        }
        //插入数据
        Dict dict = new Dict();
        dict.setDictId(dictAddReq.getDictId());
        dict.setDictName(dictAddReq.getDictName());
        dict.setDictLabel(dictAddReq.getDictLabel());
        dict.setDictGroup(dictAddReq.getDictGroup());
        // 设置创建时间和更新时间
        dict.setDictCreateTime(date);
        dict.setDictUpadateTime(date);
        boolean saveResult = this.save(dict);
        if(!saveResult){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"保存失败");
        }
        return dict;
    }

    /**
     * 更新字典
     *
     * @param dictAddReq
     * @return
     */
    @Override
    public Dict updateDict(DictAddReq dictAddReq) {
        // 获取系统当前时间，即为默认创建时间和更新时间
        Date date = new Date();
        // 设置更新时间
        dictAddReq.setDictUpdateTime(date);
        //查重
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_id",dictAddReq.getDictId())
                .eq("dict_name",dictAddReq.getDictName())
                .eq("dict_label",dictAddReq.getDictLabel())
                .eq("dict_group",dictAddReq.getDictGroup());
        long count = dictMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"该字典已经存在");
        }
        //插入数据
        Dict dict = dictMapper.selectById(dictAddReq.getCodeId());
        dict.setDictId(dictAddReq.getDictId());
        dict.setDictName(dictAddReq.getDictName());
        dict.setDictLabel(dictAddReq.getDictLabel());
        dict.setDictGroup(dictAddReq.getDictGroup());
        dictMapper.updateById(dict);
        return dict;
    }

    /**
     * 删除字典
     *
     * @param codeId
     * @return
     */
    @Override
    public boolean deleteDict(Integer codeId) {
        Dict dict = dictMapper.selectById(codeId);
        if(dict == null){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"该字典不存在");
        }
        if(dict.getDictIsDelete() == 1){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"该字典已被删除");
        }
        dict.setDictIsDelete(1);
        dictMapper.updateById(dict);
        return true;
    }

    /**
     * 翻译字典
     *
     * @param dictId
     * @return
     */
    @Override
    public String translateDict(String dictId) {
      QueryWrapper<Dict>  queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("dict_id",dictId);
      return dictMapper.selectOne(queryWrapper).getDictName();
    }
}




